package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.system.SysJishi;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysJishiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 计时收费规则Controller
 *
 * @author zjw
 * @date 2023-08-04
 */
@RestController
@RequestMapping("/system/jishi")
public class SysJishiController extends BaseController {
    @Autowired
    private ISysJishiService sysJishiService;

    /**
     * 查询计时收费规则列表
     */
//    @PreAuthorize("@ss.hasPermi('system:jishi:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysJishi sysJishi) {
        startPage();
        List<SysJishi> list = sysJishiService.selectSysJishiList(sysJishi);
        return getDataTable(list);
    }

    /**
     * 查询指定计时收费规则。
     *
     * @param sysJishi
     * @return
     */
    @GetMapping
    public AjaxResult getJishiRule(SysJishi sysJishi) {
        SysJishi result = sysJishiService.selectSysJishiByScenicId(sysJishi.getScenicId());
        return result == null ? error("计时收费规则未设置") : success(result);
    }

    /**
     * 导出计时收费规则列表
     */
//    @PreAuthorize("@ss.hasPermi('system:jishi:export')")
    @Log(title = "计时收费规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysJishi sysJishi) {
        List<SysJishi> list = sysJishiService.selectSysJishiList(sysJishi);
        ExcelUtil<SysJishi> util = new ExcelUtil<SysJishi>(SysJishi.class);
        util.exportExcel(response, list, "计时收费规则数据");
    }

    /**
     * 获取计时收费规则详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:jishi:query')")
    @GetMapping(value = "/{jishiId}")
    public AjaxResult getInfo(@PathVariable("jishiId") int jishiId) {
        return success(sysJishiService.selectSysJishiByJishiId(jishiId));
    }

    /**
     * 新增计时收费规则
     */
//    @PreAuthorize("@ss.hasPermi('system:jishi:add')")
    @Log(title = "计时收费规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysJishi sysJishi) {
        sysJishi.setCreateBy(getUsername());
        sysJishi.setCreateTime(DateUtils.getNowDate());
        return toAjax(sysJishiService.insertSysJishi(sysJishi));
    }

    /**
     * 修改计时收费规则
     */
//    @PreAuthorize("@ss.hasPermi('system:jishi:edit')")
    @Log(title = "计时收费规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysJishi sysJishi) {
        sysJishi.setUpdateBy(getUsername());
        sysJishi.setUpdateTime(DateUtils.getNowDate());
        return toAjax(sysJishiService.updateSysJishi(sysJishi));
    }

    /**
     * 删除计时收费规则
     */
//    @PreAuthorize("@ss.hasPermi('system:jishi:remove')")
    @Log(title = "计时收费规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jishiIds}")
    public AjaxResult remove(@PathVariable Long[] jishiIds) {
        return toAjax(sysJishiService.deleteSysJishiByJishiIds(jishiIds));
    }
}
